from mohe.core.obj import obj_trade
from research.calcor.pos import original_close
class hold_close(original_close):
    def __init__(self,name="",hold_period=3):
        self.hold_period=hold_period
        self.count=0
        self.timekey=None
        if name=="":
            self.name = type(self).__name__
        else:
            self.name = type(self).__name__+"("+str(name)+")"
    def reset(self):
        self.count = 0
        self.timekey = None
    def onbar(self,ss,b):
        if ss.pos=="buy":
            if self.timekey!=b["timekey"]:
                self.count=self.count+1
                if self.count==self.hold_period:
                    order={"obj":b["obj"],"timekey":b["timekey"],"price":b["close"],"action": "closesell",
                         "info": self.name+"到期平仓"+str(self.hold_period)}
                    ss.signals_data.append(order)
                    ss.pos = None
                    self.reset()
                    return True,"closesell",order["info"],self.open_price,self.open_price
            return False, "","",self.open_price,self.open_price
        elif ss.pos=="sell":
            if self.timekey!=b["timekey"]:
                self.count=self.count+1
                if self.count==self.hold_period:
                    order={"obj":b["obj"],"timekey":b["timekey"],"price":b["close"],"action": "closebuy",
                         "info": self.name+"到期平仓"+str(self.hold_period)}
                    ss.signals_data.append(order)
                    ss.pos = None
                    self.reset()
                    return True, "closebuy",order["info"],self.open_price,self.open_price
            return False, "", "",self.open_price, self.open_price
        self.timekey=b["timekey"]
        return False, "","",None,None
class overnight_close:
    def __init__(self,overnight_flag=3,delta=1,obj=None,obj_trade_tool=None,name=""):
        self.overnight_flag=overnight_flag
        if obj_trade_tool is not None:
            self.obj_trade_tool=obj_trade_tool
        else:
            self.obj_trade_tool = obj_trade(obj=obj, delta=delta)
        if name=="":
            self.name = type(self).__name__
        else:
            self.name = type(self).__name__+"("+str(name)+")"
    def reset(self):
        pass
    def onbar(self,ss,b,delta=None):
        r = self.obj_trade_tool.isovernight(b["timekey"],delta=delta)
        if r[0] and r[1]>=self.overnight_flag:
            openprice=ss.signals_data[-1]["price"]
            if ss.pos=="buy":
                order={"obj":b["obj"],"timekey":b["timekey"],"price":b["close"],"action": "closesell",
                     "info": self.name+"过夜平仓"+str(r[1])}
                ss.signals_data.append(order)
                ss.pos = None
                return r[0],r[1],"closesell",openprice,openprice
            elif ss.pos=="sell":
                order={"obj":b["obj"],"timekey":b["timekey"],"price":b["close"],"action": "closebuy",
                     "info": self.name+"过夜平仓"+str(r[1])}
                ss.signals_data.append(order)
                ss.pos = None
                return r[0], r[1], "closebuy",openprice,openprice
        return False,r[1],"",None,None